package com.example.newsapp.ui.screens.otherScreens

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.example.newsapp.data.compositionLocal.LocalNavController
import com.example.newsapp.data.database.NewsItems
import com.example.newsapp.data.`object`.StateSave
import com.example.newsapp.ui.components.otherComponent.newsDetailComponent.BodyNewsDetails
import com.example.newsapp.ui.components.otherComponent.newsDetailComponent.TopBarNewsDetails

/**
 * 新闻详情页
 *
 * @param newsItems 新闻对象
 */
@Composable
fun NewsDetailsScreen(
    newsItem: NewsItems,
    modifier: Modifier = Modifier
) {
    val context = LocalContext.current
    val navController = LocalNavController.current
    var pictureId = 0
    val picture = newsItem.picture
    val source = newsItem.source
    val title = newsItem.title
    val text = newsItem.text
    StateSave.gesturesEnable = false

    // 将获取到的picture值转换为对应的id
    if (picture != "") {
        pictureId = context.resources.getIdentifier(
            picture.substringAfter("R.drawable."),
            "drawable",
            context.packageName
        )
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(horizontal = 18.dp, vertical = 10.dp),
    ) {
        val textList = text.split("  ")

        // 顶部栏
        TopBarNewsDetails(navController = navController)
        // 新闻体
        BodyNewsDetails(
            title = title,
            source = source,
            pictureId = pictureId,
            textList = textList
        )
    }
}